﻿/// <reference path="ADS.js" />

//关闭当前窗口，刷新父窗口
function goBack() {
    window.close();
    window.opener.location.reload();
}

//Size  Templete
ADS.addEvent(window, 'load', sizeTemplete);
function sizeTemplete() {
    //获取页面内已选择的值
    ADS.addEvent(ADS.$('templeteSubmit'), 'click', function () {
        var ckbs = ADS.getElementByClassName('sizeitem', 'input');
        var value = '';
        for (var i = 0; i < ckbs.length; i++) {
            if (ckbs[i].checked) {
                var id = ckbs[i].id;
                value += '$' + id + ',' + ADS.$('body_' + id).value + ',' + ADS.$('finished_' + id).value;
            }
        }
        if (value.length = 0) {
            alert('No Templete was Designed!!');
        }
        else {
            value = value.substr(1, value.length - 1);

            ADS.$('hfvalue').value = value;
        }
    });
}


//init Templete 1,1,1$2,1,0
function initTemplete(value) {
    if (value.length != 0) {
        var templete = value.split('$');
        for (var i = 0; i < templete.length; i++) {
            var list = templete[i].split(',');
            ADS.$(list[0]).checked = true;
            ADS.$('body_' + list[0]).value = list[1];
            ADS.$('finished_' + list[0]).value = list[2];
        } 
    }
}


/*--------select menu----------*/
function selectMenu() {
    //初始化
    var list=window.opener.document.getElementById('menuList');
    var menu =list.value.split(',');
    var items = ADS.getElementByClassName('menu', 'input');
    for (var i = 0; i < menu.length; i++) {
        for (var j = 0; j < items.length; j++) {
            if (menu[i] == items[j].id) {
                items[j].checked = true;
            }
        }
    }
    //选择
    var parents = ADS.getElementByClassName('parent', 'input');
    for (var i = 0; i < parents.length; i++) {
        ADS.addEvent(parents[i], 'click', function () {
            var items = document.getElementsByName('item' + this.id);
            for (var j = 0; j < items.length; j++) {
                items[j].checked = this.checked;
            }
        });
    }
    var aitem = ADS.getElementByClassName('item', 'input');
    for (var i = 0; i < aitem.length; i++) {
        ADS.addEvent(aitem[i], 'click', function () {
            var pid = this.name.substring(4, this.name.length);
            ADS.$(pid).checked = false;
            var item = document.getElementsByName(this.name);
            for (var j = 0; j < item.length; j++) {
                if (item[j].checked == true) {
                    ADS.$(pid).checked = true; 
                    break;
                }

            }
        });
    }

    //获取
        ADS.addEvent(ADS.$('btnSelect'), 'click', function () {
            var list = '';
            for (var i = 0; i < items.length; i++) {
                if (items[i].checked == true) {
                    list += ',' + items[i].id;
                }
            }
            if (list.indexOf(',') == 0) {
                list = list.substr(1, list.length - 1);
            }
            opener.document.getElementById('menuList').value = list;
            window.close();
        });
}